package com.cc.dao.impl;

import com.cc.dao.BaseDao;
import com.cc.dao.UserDao;
import com.cc.entity.User;

import java.sql.ResultSet;
import java.sql.SQLException;

public class UserDaoImpl extends BaseDao implements UserDao {
    @Override
    public User findUserByNameAndPassword(String username, String password) {
        String sql = "select * from users where username=? and password=?";
        Object[] params = {username, password};
        ResultSet rs = executeQuery(sql, params);
        User user = null;
        if (rs != null) {
            try {
                while (rs.next()) {
                    user = new User();
                    user.setId(rs.getInt("id"));
                    user.setUsername(rs.getString("username"));
                    user.setPassword(rs.getString("password"));
                    user.setEmail(rs.getString("email"));
                    user.setCreated_at(rs.getTimestamp("created_at"));
                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            } finally {
                closedAll(rs);
            }
        }
        return user;
    }
}
